//
//  RootViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/5.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "RootViewController.h"
#import "HomePageViewController.h"
#import "ClassificationViewController.h"
#import "FunctionViewController.h"
#import "AboutViewController.h"
#import "VAGuideView.h"
#import "UIImage+Ex.h"
#import "AffectionateViewController.h"
@interface RootViewController ()
{
    VAGuideView *_guideView;
    UIInterfaceOrientation _lockRotate;
    UIInterfaceOrientation _currInterface;
}
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UITabBar appearance]setTintColor:[UIColor redColor]];
    [self ShowGuide];
}


-(void)ShowGuide{
    if ([VAGuideView shouldShowGuide]) {
        self.navigationController.navigationBar.hidden =YES;
        NSMutableArray *arrIamge=[NSMutableArray array];
#pragma mark - 这里填你们到导航图  for循环控制张数   后面是图片的名字
        for (int i = 0; i < 5; i++) {
            UIImage *image = [UIImage imageWithFileName:[NSString stringWithFormat:@"%@-guide-%d-v", @"iPhone5", i] ofType:@"png"];
            
            [arrIamge addObject:image];
        }
        _guideView = [VAGuideView guideViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) arrImages:arrIamge space:30];
        //引导页背景图
        UIImageView *imageViewBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
#pragma mark - 这里是播放导航图时的背景图片  换成你们自己的即可
        imageViewBg.image = [UIImage imageNamed:@"Love"];
        imageViewBg.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        imageViewBg.contentMode = UIViewContentModeScaleAspectFill;
        imageViewBg.clipsToBounds = YES;
        [_guideView insertSubview:imageViewBg atIndex:0];
        // 适配引导视图的页面显示
        _guideView.imageContentMode = UIViewContentModeLeft | UIViewContentModeRight | UIViewContentModeTop | UIViewContentModeBottom;
        
        //最后一页进入应用按钮
        UIButton *btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnStart setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
#pragma mark - 这是最后一页导航图上面按钮的控制，图片，文字，位置 点击方法（点击之后才创建首页视图,因为导航图消失时有动画效果，建议加个定时器，1秒之后才开始创建视图）
        [btnStart setBackgroundImage:[UIImage imageNamed:@"guide"] forState:UIControlStateNormal];
        [btnStart setTitle:@"开始体验" forState:UIControlStateNormal];
        btnStart.frame = CGRectMake(0, self.view.frame.size.height - 90, 150, 45);
        btnStart.center = CGPointMake(self.view.frame.size.width/2,btnStart.center.y);
        [btnStart addTarget:self action:@selector(touchStartButton) forControlEvents:UIControlEventTouchUpInside];
        btnStart.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
        [_guideView addButtonAtLastPage:btnStart];
        if (_guideView && arrIamge.count>0) {
            _currInterface = [[UIApplication sharedApplication] statusBarOrientation];
            _lockRotate = YES;
            [self.view addSubview:_guideView];
            double delayInSeconds = 0;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [_guideView startAnimationWithDuration:3
                                 animationComplecation:nil
                                            didDismiss:^{
                                                _lockRotate = NO;
                                            }];
            });
        }
        
    }
#pragma mark - 不是第一次启动，直接创建本页面视图
    else
    {
        _currInterface = [[UIApplication sharedApplication] statusBarOrientation];
        _lockRotate = YES;
        [self creatController];
    }

}
-(void)touchStartButton
{
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(creatController) userInfo:nil repeats:NO];
}
-(void)creatController{
    HomePageViewController *homeVC=[[HomePageViewController alloc]init];
    UINavigationController *homeTV=[[UINavigationController alloc]initWithRootViewController:homeVC];
    homeTV.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"tabbar"] tag:101];
    
    ClassificationViewController *classVC=[[ClassificationViewController alloc]init];
    UINavigationController *classTV=[[UINavigationController alloc]initWithRootViewController:classVC];
    classTV.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"分类" image:[UIImage imageNamed:@"tabb"] tag:102];
    AffectionateViewController *affVC = [[AffectionateViewController alloc]init];
    UINavigationController *affNC =  [[UINavigationController alloc]initWithRootViewController:affVC];
    affNC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"恩爱说" image:[UIImage  imageNamed:@"10.png"] tag:103];
    
    FunctionViewController *funcVC=[[FunctionViewController alloc]init];
    UINavigationController *funcTV=[[UINavigationController alloc]initWithRootViewController:funcVC];
    funcTV.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"助手" image:[UIImage imageNamed:@"tab"] tag:100];
    
    AboutViewController *aboutVC=[[AboutViewController alloc]init];
    UINavigationController *aboutTV=[[UINavigationController alloc]initWithRootViewController:aboutVC];
    aboutTV.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"关于" image:[UIImage imageNamed:@"tabba"] tag:100];
    
    self.viewControllers=@[homeTV,classTV,affNC,funcTV,aboutTV];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
